package wangm.service;

import wangm.bean.ExaminationTopic;
import wangm.repository.ExaminationPaperTopicRepository;
import wangm.repository.ExaminationTopicOptionRepository;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 小题service
 *
 * @author zk
 * @date 2018-02-23 11:16
 */
@Service
public class ExaminationTopicServiceImp {
    private static Logger logger = LoggerFactory.getLogger(ExaminationTopicServiceImp.class);
    /**
     * 试卷、小题 repository
     */
    @Autowired
    private ExaminationPaperTopicRepository paperTopicRepository;
    /**
     * 小题、选项 repository
     */
    @Autowired
    private ExaminationTopicOptionRepository topicOptionRepository;
    /**
     * 根据paperId 返回list<小题></>
     *
     * @param paperId paperId
     * @return List<PaperTopicBean></>
     */
    public List<ExaminationTopic> getTopicByPaperId(String paperId){
        //获取小题列表(仅小题内容，无选项，无答案)
        List<ExaminationTopic> result = paperTopicRepository.getTopicListByPid(paperId);
        //组合 选项
        for (int i = 0; i < result.size(); i++) {
            ExaminationTopic topicBean = result.get(i);
            topicBean.setOptions(topicOptionRepository.getOptionListByTid(topicBean.getId()));
        }
        return result;
    }
    /**
     * 根据paperId 返回带答案的list<小题></>
     *
     * @param paperId paperId
     * @return List<PaperTopicBean></>
     */
    public List<ExaminationTopic> getTopicByPaperWithAnswerId(String paperId){
        //获取小题列表(仅小题内容，无选项，无答案)
        List<ExaminationTopic> result = paperTopicRepository.getTopicListByPid(paperId);
        //组合 选项，答案
        for (int i = 0; i < result.size(); i++) {
            ExaminationTopic topicBean = result.get(i);
            //添加正确答案
            topicBean.setRightOptionIds(topicOptionRepository.getAnswerListByTid(topicBean.getId()).toArray(new String[0]));
            //添加选项
            topicBean.setOptions(topicOptionRepository.getOptionListByTid(topicBean.getId()));
        }
        return result;
    }

}
